<?php

if (!defined('SMF'))
	die('Hacking attempt...');

function snet_admin_info()
{
	global $context, $sourcedir;
	
	loadLanguage('SimpleNetwork-Admin');
	loadTemplate('/snet/SN-Admin');
	require_once($sourcedir . '/snet/Subs-SimpleNetwork.php');
	
	$sub_actions = array(
		'home' => 'snet_admin_info_home',
		'upgrades' => 'snet_admin_info_upgrades',		
	);
	
	$context['template_layers'][] = 'snet_admin_info';
	
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($sub_actions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'home';
	$sub_actions[$_REQUEST['sa']]();	
}

function snet_admin_info_home()
{
	global $context, $txt;
	
	// Stats
	$context['num_users'] = snet_count_num_users();
	$context['num_status_updates'] = snet_count_num_status_updates();
	$context['num_comments'] = snet_count_num_comments();	
	
	$context['page_title'] = $txt['snet_admin_info_home'] . ' | ' . $txt['snet_admin_title'];
	$context['sub_template'] = 'snet_admin_info_home';	
}

function snet_admin_info_upgrades()
{
	global $context, $scripturl, $sourcedir, $txt, $settings;
	
	$context['page_title'] = $txt['snet_admin_info_upgrades'] . ' | ' . $txt['snet_admin_title'];

	$listOptions = array(
		'id' => 'snet_upgrade_log',
		'items_per_page' => 10,
		'base_href' => $scripturl . '?action=admin;area=snet-info;sa=upgrades',
		'default_sort_col' => 'date',
		'get_items' => array(
			'function' => 'snet_load_upgrade_log',
			'params' => array(),
		),
		'get_count' => array(
			'function' => 'snet_count_num_upgrades',
			'params' => array(),
		),
		'columns' => array(
			'from_version' => array(
				'header' => array(
					'value' => $txt['snet_admin_from_version'],
				),
				'data' => array(
					'db' => 'from_version',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'from_version',
					'reverse' => 'from_version DESC',
				),
			),
			'to_version' => array(
				'header' => array(
					'value' => $txt['snet_admin_to_version'],
				),
				'data' => array(
					'db' => 'to_version',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'to_version',
					'reverse' => 'to_version DESC',
				),
			),
			'date' => array(
				'header' => array(
					'value' => $txt['snet_admin_date'],
				),
				'data' => array(
					'db' => 'date',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'date DESC',
					'reverse' => 'date',
				),
			),
			'time_taken' => array(
				'header' => array(
					'value' => $txt['snet_admin_elapsed_time'] . '&nbsp;
					<a href="http://localhost/smf2/index.php?action=helpadmin;help=snet_admin_help_upg_log_time" onclick="return reqWin(this.href);" class="help snet_icon" title="'.$txt['help'].'"><img src="'.$settings['images_url'].'/helptopics.gif" alt="'.$txt['help'].'" /></a>',
				),
				'data' => array(
					'db' => 'elapsed_time',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'elapsed_time',
					'reverse' => 'elapsed_time DESC',					
				),
			),
			'upgrader' => array(
				'header' => array(
					'value' => $txt['snet_admin_upgrader'],
				),
				'data' => array(
					'sprintf' => array(
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
						'params' => array(
							'id_member' => false,
							'member_name' => false,
						),
					),
				),
				'sort' => array(
					'default' => 'mem.real_name',
					'reverse' => 'mem.real_name DESC',
				),
			),
		),
		'form' => array(
			'href' => $scripturl . '?action=admin;area=snet-info;sa=upgrades',
			'include_start' => true,
			'include_sort' => true,
		),
		'additional_rows' => array(
			array(),
		),
	);

	require_once($sourcedir . '/Subs-List.php');
	createList($listOptions);

	$context['sub_template'] = 'show_list';
	$context['default_list'] = 'snet_upgrade_log';	
}

function snet_admin_settings()
{
	global $context, $txt, $scripturl, $modSettings, $settings, $sourcedir;
	
	loadLanguage('SimpleNetwork-Admin');
	loadLanguage('SimpleNetwork');
	loadTemplate('/snet/SN-Admin');
	
	$context['page_title'] = $txt['snet_admin_settings'] . ' | ' . $txt['snet_admin_title'];
	
	$sub_actions = array(
		'general' => 'snet_admin_settings_general',
		'posting' => 'snet_admin_settings_posting',
		'social_options' => 'snet_admin_settings_social_options',		
	);
	
	// Because other clever people have already done all the work for me ;)
	require_once($sourcedir . '/ManageSettings.php');
	loadGeneralSettingParameters($sub_actions, 'general');
	
	// Tabs, like... like on a guitar! :D
	$context[$context['admin_menu_name']]['tab_data'] = array(
		'title' => $txt['snet_admin_button_settings'],
		'help' => 'snet_settings_general',
		'description' => $txt['snet_admin_settings_desc'],
		'tabs' => array(
			'general' => array(
			),
			'posting' => array(
				'description' => $txt['snet_admin_settings_posting_desc'],
			),	
			'social_options' => array(
				'description' => $txt['snet_admin_settings_social_options_desc'],
			),				
		),
	);

	// Call the right function for this sub-acton.
	$sub_actions[$_REQUEST['sa']]();
}

function snet_admin_settings_general($return_config = false)
{
	global $txt, $scripturl, $context, $settings, $modSettings;

	$config_vars = array(
			// Master setting.
			array('check', 'snet_enable', 'javascript' => ' onchange="javascript:snet_enable();"'),
		'',
			// Privacy settings (will probably be replaced by permissions later on)
			array('select', 'snet_enabled_for', array($txt['snet_enabled_for_all'],$txt['snet_enabled_for_choose_defon'],$txt['snet_enabled_for_choose_defoff']), 'disabled' => empty($modSettings['snet_enable'])),	
	);

	if ($return_config)
		return $config_vars;

	// Saving?
	if (isset($_GET['save']))
	{
		checkSession();

		saveDBSettings($config_vars);

		writeLog();
		redirectexit('action=admin;area=snet-settings;sa=general');
	}

	$context['post_url'] = $scripturl . '?action=admin;area=snet-settings;sa=general;save';
	$context['settings_title'] = '<img src="' . $settings['images_url'] . '/simplenetwork/general.png" alt="" class="snet_icon" /> ' . $txt['snet_admin_button_settings_general'];

	prepareDBSettingContext($config_vars);
}

function snet_admin_settings_posting($return_config = false)
{
	global $txt, $scripturl, $context, $settings, $modSettings, $sourcedir;
	
	$auto_embedder = file_exists($sourcedir . '/AutoEmbedMediaPro2.php') || file_exists($sourcedir . '/Aeva-Subs.php');

	$config_vars = array(
			// BBC and smileys
			array('check', 'snet_enable_bbc', 'disabled' => empty($modSettings['snet_enable']) || empty($modSettings['enableBBC'])),
			array('check', 'snet_enable_smileys', 'disabled' => empty($modSettings['snet_enable'])),			
		'',
			// Cross-profile posting, or whatever you'd call it.
			array('check', 'snet_enable_wall_posts', 'subtext' => $txt['snet_enable_wall_posts_info'], 'disabled' => empty($modSettings['snet_enable'])),		
		'',		
			// Limits and stuffz
			array('int', 'snet_max_status_length', 'subtext' => $txt['snet_max_length_info'], 'postinput' => $txt['snet_characters'],'disabled' => empty($modSettings['snet_enable'])),
			array('int', 'snet_max_comment_length', 'subtext' => $txt['snet_max_length_info'], 'postinput' => $txt['snet_characters'], 'disabled' => empty($modSettings['snet_enable'])),
		'',
			// Different types of status updates
			array('check', 'snet_enable_text', 'disabled' => empty($modSettings['snet_enable'])),
			array('check', 'snet_enable_photo', 'disabled' => empty($modSettings['snet_enable'])),
			array('check', 'snet_enable_link', 'disabled' => empty($modSettings['snet_enable'])),
			array('check', 'snet_enable_quote','disabled' => empty($modSettings['snet_enable'])),
			array('check', 'snet_enable_video', 'subtext' => $txt['snet_enable_video_info'], 'disabled' => (empty($modSettings['snet_enable']) || !$auto_embedder)),			
	);

	if ($return_config)
		return $config_vars;

	// Saving?
	if (isset($_GET['save']))
	{
		checkSession();

		saveDBSettings($config_vars);

		writeLog();
		redirectexit('action=admin;area=snet-settings;sa=posting');
	}

	$context['post_url'] = $scripturl . '?action=admin;area=snet-settings;sa=posting;save';
	$context['settings_title'] = '<img src="' . $settings['images_url'] . '/simplenetwork/posting.png" alt="" class="snet_icon" /> ' . $txt['snet_admin_button_settings_posting'];

	prepareDBSettingContext($config_vars);
}

function snet_admin_settings_social_options($return_config = false)
{
	global $txt, $scripturl, $context, $settings, $modSettings;
	
	$context['settings_message'] = '<img src="' . $settings['default_images_url'] . '/simplenetwork/tip.png" alt="" class="floatleft"/>
	<span class="floatleft snet_tip_text">&nbsp;<strong>' . $txt['snet_tip'] . ':</strong> ' . sprintf($txt['snet_social_buttons_tip'],$scripturl . '?action=admin;area=languages;' . $context['session_var'] . '=' .$context['session_id']) . '
	(<a id="snet_social_buttons_tip_popup" href="' .$scripturl .'?action=helpadmin;help=snet_social_buttons_tip_popup" onclick="return reqWin(this.href);" title="' .$txt['help'] .'">?</a>)</span>';

	$config_vars = array(
			// Like/dislike buttons.
			array('check', 'snet_enable_like_buttons', 'disabled' => empty($modSettings['snet_enable'])),
			array('select', 'snet_enable_dislike_buttons', array($txt['snet_enable_dislike_buttons_no'],$txt['snet_enable_dislike_buttons_yes'],$txt['snet_enable_dislike_buttons_yes_anonymous']), 'disabled' => empty($modSettings['snet_enable'])),
		'',
			// Like/dislike buttons on comments
			array('check', 'snet_enable_like_buttons_comments', 'disabled' => empty($modSettings['snet_enable']) || empty($modSettings['snet_enable_like_buttons'])),
			array('check', 'snet_enable_dislike_buttons_comments', 'disabled' => empty($modSettings['snet_enable']) || $modSettings['snet_enable_dislike_buttons'] == 0),
	);

	if ($return_config)
		return $config_vars;

	// Saving?
	if (isset($_GET['save']))
	{
		checkSession();

		saveDBSettings($config_vars);

		writeLog();
		redirectexit('action=admin;area=snet-settings;sa=social_options');
	}

	$context['post_url'] = $scripturl . '?action=admin;area=snet-settings;sa=social_options;save';
	$context['settings_title'] = '<img src="' . $settings['images_url'] . '/simplenetwork/social_options.png" alt="" class="snet_icon" /> ' . $txt['snet_admin_button_settings_social_options'];

	prepareDBSettingContext($config_vars);
}

function snet_load_upgrade_log($start, $items_per_page, $sort, $where = false, $where_params = array())
{
	global $smcFunc;

	$request = $smcFunc['db_query']('', '
		SELECT
			upg.id_upgrade, upg.from_version, upg.to_version, upg.date, upg.elapsed_time, upg.id_member,
			mem.real_name
		FROM {db_prefix}snet_upgrades AS upg
			LEFT JOIN {db_prefix}members AS mem ON (upg.id_member = mem.id_member)
		ORDER BY {raw:sort}
		LIMIT {int:start}, {int:per_page}',
		array(
			'sort' => $sort,
			'start' => $start,
			'per_page' => $items_per_page,
		)
	);

	$upgrades = array();
	while ($row = $smcFunc['db_fetch_assoc']($request))
	{
		$upgrades[$row['id_upgrade']] = array(
			'from_version' => $row['from_version'] != '0' ? $row['from_version'] : '<em>(none)</em>',
			'to_version' => $row['to_version'],
			'date' => timeformat($row['date']),
			'elapsed_time' => round($row['elapsed_time'],2) . 'ms',
			'id_member' => $row['id_member'],
			'member_name' => $row['real_name'],			
		);
	}
	$smcFunc['db_free_result']($request);

	return $upgrades;
}

function snet_count_num_upgrades()
{
	global $smcFunc, $modSettings;

	$request = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}snet_upgrades',
		array()
	);
	list ($num_upgrades) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $num_upgrades;
}

function snet_count_num_users()
{
	global $smcFunc, $modSettings;

	$request = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}snet_users
		WHERE enabled = 1',
		array()
	);
	list ($num_users) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $num_users;
}

function snet_count_num_status_updates()
{
	global $smcFunc, $modSettings;

	$request = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}snet_status_updates
		WHERE type != 6',
		array()
	);
	list ($num_status_updates) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $num_status_updates;
}

function snet_count_num_comments()
{
	global $smcFunc, $modSettings;

	$request = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}snet_comments',
		array()
	);
	list ($num_comments) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);

	return $num_comments;
}

?>